package com.whty.module.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.whty.common.annotation.Log;
import com.whty.common.enums.BusinessType;
import com.whty.mariotools.DateUtil;
import com.whty.module.domain.DepotHead;
import com.whty.module.domain.Product;
import com.whty.module.service.IDepotHeadService;
import com.whty.web.core.base.BaseController;
import com.whty.framework.web.page.TableDataInfo;
import com.whty.common.base.AjaxResult;

/**
 * 入库记录 信息操作处理
 * 
 * @author mario
 * @date 2018-10-31
 */
@Controller
@RequestMapping("/module/depotHead")
public class DepotHeadController extends BaseController
{
    private String prefix = "module/depotHead";
    private String prefix_detail = "module/depotHeadItem";
	@Autowired
	private IDepotHeadService depotHeadService;
	  final static Logger logger = LoggerFactory.getLogger(DepotHeadController.class);
	
	@RequiresPermissions("module:depotHead:view")
	@GetMapping()
	public String depotHead()
	{
	    return prefix + "/depotHead";
	}
	
	/**
	 * 查询入库记录列表
	 */
	@RequiresPermissions("module:depotHead:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(DepotHead depotHead)
	{
		startPage();
        List<DepotHead> list = depotHeadService.selectDepotHeadList(depotHead);
		return getDataTable(list);
	}
	
	/**
	 * 新增入库记录
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 
	 * 新增保存入库记录
	 */
	@RequiresPermissions("module:depotHead:add")
	@Log(title = "入库记录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(@RequestParam("itemFileName")MultipartFile itemFileName,String amount,String tagType,String remark,
            HttpServletRequest request,HttpServletResponse response)
	{	 

		String fileName = "";
		String result = "";
		 try {
		
			//判断文件是否为空
	        if(itemFileName!=null) {
	        	fileName=itemFileName.getOriginalFilename();
	            //进一步判断文件是否为空（即判断其大小是否为0或其名称是否为null）
	            long size=itemFileName.getSize();
	            if(fileName!=null || !("").equals(fileName) && size!=0) {
	            	 //批量导入。参数：文件名，文件。
	            	int nums=Integer.parseInt(amount);
	                result=depotHeadService.batchImport(itemFileName, tagType, nums, remark, fileName);
                    logger.info("导入的结果是："+result);
	            }
	        }
		 } catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
	}
         if(!result.equals("00")){
        	 return AjaxResult.error(result);
         }
		 return AjaxResult.success(result);
	}

	/**
	 * 修改入库记录
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		DepotHead depotHead = depotHeadService.selectDepotHeadById(id);
		mmap.put("depotHead", depotHead);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存入库记录
	 */
	@RequiresPermissions("module:depotHead:edit")
	@Log(title = "入库记录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(DepotHead depotHead)
	{		
		return toAjax(depotHeadService.updateDepotHead(depotHead));
	}
	
	/**
	 * 删除入库记录
	 */
	@RequiresPermissions("module:depotHead:remove")
	@Log(title = "入库记录", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(depotHeadService.deleteDepotHeadByIds(ids));
	}
	
	 /**
     * 查询产品详细
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap)
    {
        mmap.put("depotHead",  depotHeadService.selectDepotHeadById(id));
        mmap.put("depotHeadList",depotHeadService.selectDepotHeadList(new DepotHead()));
        return prefix_detail+"/depotHeadItem";
    }
}
